﻿using Edu.Pages;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Extensions.Options;
using System.Net.Http;
using Newtonsoft.Json;

namespace Edu.Web.Pages.ImportData
{
    public enum ExcelType
    {
        Teacher,
        Student,
        Task
    }


    public class ImportData
    {
        [Display(Name = "上传文件")]
        public IFormFile File { get; set; }
        public ExcelType Type { get; set; }
    }


    public enum ImportResult
    {
        None,
        Ok,
        Error,
    }


    public class IndexModel : EduPageModelBase
    {
        private readonly string fileDir;

        [BindProperty]
        public ImportData ImportData { get; set; }


        private static ImportResult lastImportResult = ImportResult.None;

        public static ImportResult LastImportResult
        {
            get
            {
                if(lastImportResult != ImportResult.None)
                {
                    var current = lastImportResult;
                    lastImportResult = ImportResult.None;
                    return current;
                }
                return ImportResult.None;
            }
        }

        public static string ImportResultMessage;

        public IndexModel(IOptions<ExcelOptions> excelOptions)
        {
           this.fileDir =  excelOptions.Value.Path;
        }


        public void OnGet()
        {
        }

   
        public async Task<IActionResult> OnPostAsync()
        {
            if (ImportData.File == null)
            {
                return NoContent();
            }
            string path="";
            string url = "";
            switch (this.ImportData.Type)
            {
                case ExcelType.Teacher:
                    path = Path.Combine(fileDir, "teacher.xlsx");
                    url = "http://127.0.0.1:6000/api/excel/teacher";
                    break;
                case ExcelType.Student:
                    path = Path.Combine(fileDir, "student.xlsx");
                    url = "http://127.0.0.1:6000/api/excel/student";
                    break;
                case ExcelType.Task:
                    path = Path.Combine(fileDir, "task.xlsx");
                    url = "http://127.0.0.1:6000/api/excel/task";
                    break;
                default:
                    break;
            }
            using (FileStream stream = new FileStream(path, FileMode.Create))
            {
                await ImportData.File.CopyToAsync(stream);
            }

            using (var httpClient = new HttpClient())
            {
                var response = await httpClient.GetAsync(url);

                response.EnsureSuccessStatusCode();

                var result = await response.Content.ReadAsStringAsync();

                var definition = new { result = 0, message = ""};

                var data = JsonConvert.DeserializeAnonymousType(result, definition);

                if(data.result == 500)
                {
                    lastImportResult = ImportResult.Error;
                    ImportResultMessage = data.message;
                }
                else if(data.result == 200)
                {
                    lastImportResult = ImportResult.Ok;
                    ImportResultMessage = "导入成功";
                }
            }
            return Redirect("/ImportData");
        }
    }


    

}